"""
@author: Gaoyaoxia
@date: 2020-08-04 17:14:32
"""
<!-- 情感检索环状图 -->
<template>
  <div :id="id" :style="{height: height, width: width}"></div>
</template>

<script>
import echarts from "echarts";
export default {
  props: {
    id: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "260px",
    },
    data: {
      type: Array,
      default: () => [],
    },
    title: {
      type: String,
      default: "",
    },
    color: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      chart: null,
      legend1: [],
      legend2: [],
      legend3: [],
      legend4: [],
      legend5: [],
    };
  },
  methods: {
    getlegendData() {
      if (this.data.length === 0) {
        return;
      }
      this.legend1 = [];
      this.legend2 = [];
      this.legend3 = [];
      this.legend4 = [];
      this.legend5 = [];
      this.data.forEach((item, index) => {
        if (index < 5) {
          this.legend1.push(item.name);
        } else {
          this.legend2.push(item.name);
        }
      });
    },
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id));
      const option = {
        title: {
          text: "",
        },
        tooltip: {
          trigger: "item",
          formatter: "{b} :<br/>负面 {d}%",
        },
        legend: [
          {
            type: "scroll",
            left: "3%",
            top: "5%",
            orient: "vertical",
            itemWidth: 12,
            itemHeight: 12,
            itemGap: 20,
            data: this.legend1,
          },
          {
            type: "scroll",
            left: "20%",
            top: "5%",
            orient: "vertical",
            itemWidth: 12,
            itemHeight: 12,
            itemGap: 20,
            data: this.legend2,
          },
        ],
        series: [
          {
            type: "funnel",
            width: "40%",
            height: "70%",
            top: "2%",
            left: "50%",
            sort: "dascending",
            label: {
              show: false,
              emphasis: {
                show: false,
              },
            },
            data: this.data,
          },
        ],
        color: [
          "#F24D4B",
          "#FFAD3D",
          "#5BDA79",
          "#1FAFF5",
          "#9481F5",
          "#FF7D3D",
          "#66BB6A",
          "#25F1D8",
          "#4B6EF2",
          "#FF5AA4",
        ],
      };
      this.chart.setOption(option);
    },
  },
  mounted() {
    this.getlegendData();
    this.initChart();
  },
  destroyed() {
    if (!this.chart) {
      return;
    }
    this.chart = null;
  }, //生命周期 - 销毁完成
  watch: {
    data: {
      handler(newVal, oldVal){
        console.log("监听：", newVal)
        if(this.chart){
          //销毁
          this.chart.dispose();
          if(newVal){
            this.initChart();
          } 
        } else{
          this.initChart();
        }
      },
      deep: true //对象内部属性的监听
    }
  }
};
</script>
<style lang='stylus' scoped></style>